export interface Theme {
  name: string;
  label: string;
  description: string;
  colors: {
    light: Record<string, string>;
    dark: Record<string, string>;
  };
}

export const themes: Theme[] = [
  {
    name: "default",
    label: "默认主题",
    description: "经典的紫色系主题",
    colors: {
      light: {
        "--background": "#ffffff",
        "--foreground": "#312e81",
        "--card": "#ffffff",
        "--card-foreground": "#312e81",
        "--popover": "#ffffff",
        "--popover-foreground": "#312e81",
        "--primary": "#8b5cf6",
        "--primary-foreground": "#ffffff",
        "--secondary": "#f3f0ff",
        "--secondary-foreground": "#4338ca",
        "--muted": "#f5f3ff",
        "--muted-foreground": "#7c3aed",
        "--accent": "#dbeafe",
        "--accent-foreground": "#1e40af",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#e0e7ff",
        "--input": "#e0e7ff",
        "--ring": "#8b5cf6",
        "--chart-1": "#8b5cf6",
        "--chart-2": "#7c3aed",
        "--chart-3": "#6d28d9",
        "--chart-4": "#5b21b6",
        "--chart-5": "#4c1d95",
        "--sidebar": "#f5f3ff",
        "--sidebar-foreground": "#312e81",
        "--sidebar-primary": "#8b5cf6",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#dbeafe",
        "--sidebar-accent-foreground": "#1e40af",
        "--sidebar-border": "#e0e7ff",
        "--sidebar-ring": "#8b5cf6",
      },
      dark: {
        "--background": "#0f172a",
        "--foreground": "#e0e7ff",
        "--card": "#1e1b4b",
        "--card-foreground": "#e0e7ff",
        "--popover": "#1e1b4b",
        "--popover-foreground": "#e0e7ff",
        "--primary": "#8b5cf6",
        "--primary-foreground": "#ffffff",
        "--secondary": "#1e1b4b",
        "--secondary-foreground": "#e0e7ff",
        "--muted": "#1e1b4b",
        "--muted-foreground": "#c4b5fd",
        "--accent": "#4338ca",
        "--accent-foreground": "#e0e7ff",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#2e1065",
        "--input": "#2e1065",
        "--ring": "#8b5cf6",
        "--chart-1": "#a78bfa",
        "--chart-2": "#8b5cf6",
        "--chart-3": "#7c3aed",
        "--chart-4": "#6d28d9",
        "--chart-5": "#5b21b6",
        "--sidebar": "#0f172a",
        "--sidebar-foreground": "#e0e7ff",
        "--sidebar-primary": "#8b5cf6",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#4338ca",
        "--sidebar-accent-foreground": "#e0e7ff",
        "--sidebar-border": "#2e1065",
        "--sidebar-ring": "#8b5cf6",
      },
    },
  },
  {
    name: "blue",
    label: "蓝色主题",
    description: "清新的蓝色系主题",
    colors: {
      light: {
        "--background": "#ffffff",
        "--foreground": "#1e293b",
        "--card": "#ffffff",
        "--card-foreground": "#1e293b",
        "--popover": "#ffffff",
        "--popover-foreground": "#1e293b",
        "--primary": "#3b82f6",
        "--primary-foreground": "#ffffff",
        "--secondary": "#eff6ff",
        "--secondary-foreground": "#1d4ed8",
        "--muted": "#f8fafc",
        "--muted-foreground": "#64748b",
        "--accent": "#dbeafe",
        "--accent-foreground": "#1e40af",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#e2e8f0",
        "--input": "#e2e8f0",
        "--ring": "#3b82f6",
        "--chart-1": "#3b82f6",
        "--chart-2": "#2563eb",
        "--chart-3": "#1d4ed8",
        "--chart-4": "#1e40af",
        "--chart-5": "#1e3a8a",
        "--sidebar": "#f8fafc",
        "--sidebar-foreground": "#1e293b",
        "--sidebar-primary": "#3b82f6",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#dbeafe",
        "--sidebar-accent-foreground": "#1e40af",
        "--sidebar-border": "#e2e8f0",
        "--sidebar-ring": "#3b82f6",
      },
      dark: {
        "--background": "#0f172a",
        "--foreground": "#f1f5f9",
        "--card": "#1e293b",
        "--card-foreground": "#f1f5f9",
        "--popover": "#1e293b",
        "--popover-foreground": "#f1f5f9",
        "--primary": "#3b82f6",
        "--primary-foreground": "#ffffff",
        "--secondary": "#1e293b",
        "--secondary-foreground": "#f1f5f9",
        "--muted": "#1e293b",
        "--muted-foreground": "#94a3b8",
        "--accent": "#1e40af",
        "--accent-foreground": "#f1f5f9",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#334155",
        "--input": "#334155",
        "--ring": "#3b82f6",
        "--chart-1": "#60a5fa",
        "--chart-2": "#3b82f6",
        "--chart-3": "#2563eb",
        "--chart-4": "#1d4ed8",
        "--chart-5": "#1e40af",
        "--sidebar": "#0f172a",
        "--sidebar-foreground": "#f1f5f9",
        "--sidebar-primary": "#3b82f6",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#1e40af",
        "--sidebar-accent-foreground": "#f1f5f9",
        "--sidebar-border": "#334155",
        "--sidebar-ring": "#3b82f6",
      },
    },
  },
  {
    name: "green",
    label: "绿色主题",
    description: "自然的绿色系主题",
    colors: {
      light: {
        "--background": "#ffffff",
        "--foreground": "#1f2937",
        "--card": "#ffffff",
        "--card-foreground": "#1f2937",
        "--popover": "#ffffff",
        "--popover-foreground": "#1f2937",
        "--primary": "#10b981",
        "--primary-foreground": "#ffffff",
        "--secondary": "#ecfdf5",
        "--secondary-foreground": "#047857",
        "--muted": "#f9fafb",
        "--muted-foreground": "#6b7280",
        "--accent": "#d1fae5",
        "--accent-foreground": "#065f46",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#d1d5db",
        "--input": "#d1d5db",
        "--ring": "#10b981",
        "--chart-1": "#10b981",
        "--chart-2": "#059669",
        "--chart-3": "#047857",
        "--chart-4": "#065f46",
        "--chart-5": "#064e3b",
        "--sidebar": "#f9fafb",
        "--sidebar-foreground": "#1f2937",
        "--sidebar-primary": "#10b981",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#d1fae5",
        "--sidebar-accent-foreground": "#065f46",
        "--sidebar-border": "#d1d5db",
        "--sidebar-ring": "#10b981",
      },
      dark: {
        "--background": "#111827",
        "--foreground": "#f9fafb",
        "--card": "#1f2937",
        "--card-foreground": "#f9fafb",
        "--popover": "#1f2937",
        "--popover-foreground": "#f9fafb",
        "--primary": "#10b981",
        "--primary-foreground": "#ffffff",
        "--secondary": "#1f2937",
        "--secondary-foreground": "#f9fafb",
        "--muted": "#1f2937",
        "--muted-foreground": "#9ca3af",
        "--accent": "#065f46",
        "--accent-foreground": "#f9fafb",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#374151",
        "--input": "#374151",
        "--ring": "#10b981",
        "--chart-1": "#34d399",
        "--chart-2": "#10b981",
        "--chart-3": "#059669",
        "--chart-4": "#047857",
        "--chart-5": "#065f46",
        "--sidebar": "#111827",
        "--sidebar-foreground": "#f9fafb",
        "--sidebar-primary": "#10b981",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#065f46",
        "--sidebar-accent-foreground": "#f9fafb",
        "--sidebar-border": "#374151",
        "--sidebar-ring": "#10b981",
      },
    },
  },
  {
    name: "orange",
    label: "橙色主题",
    description: "温暖的橙色系主题",
    colors: {
      light: {
        "--background": "#ffffff",
        "--foreground": "#1f2937",
        "--card": "#ffffff",
        "--card-foreground": "#1f2937",
        "--popover": "#ffffff",
        "--popover-foreground": "#1f2937",
        "--primary": "#f97316",
        "--primary-foreground": "#ffffff",
        "--secondary": "#fff7ed",
        "--secondary-foreground": "#c2410c",
        "--muted": "#fafaf9",
        "--muted-foreground": "#78716c",
        "--accent": "#fed7aa",
        "--accent-foreground": "#9a3412",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#e7e5e4",
        "--input": "#e7e5e4",
        "--ring": "#f97316",
        "--chart-1": "#f97316",
        "--chart-2": "#ea580c",
        "--chart-3": "#dc2626",
        "--chart-4": "#b91c1c",
        "--chart-5": "#991b1b",
        "--sidebar": "#fafaf9",
        "--sidebar-foreground": "#1f2937",
        "--sidebar-primary": "#f97316",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#fed7aa",
        "--sidebar-accent-foreground": "#9a3412",
        "--sidebar-border": "#e7e5e4",
        "--sidebar-ring": "#f97316",
      },
      dark: {
        "--background": "#0c0a09",
        "--foreground": "#fafaf9",
        "--card": "#1c1917",
        "--card-foreground": "#fafaf9",
        "--popover": "#1c1917",
        "--popover-foreground": "#fafaf9",
        "--primary": "#f97316",
        "--primary-foreground": "#ffffff",
        "--secondary": "#1c1917",
        "--secondary-foreground": "#fafaf9",
        "--muted": "#1c1917",
        "--muted-foreground": "#a8a29e",
        "--accent": "#9a3412",
        "--accent-foreground": "#fafaf9",
        "--destructive": "#ef4444",
        "--destructive-foreground": "#ffffff",
        "--border": "#292524",
        "--input": "#292524",
        "--ring": "#f97316",
        "--chart-1": "#fb923c",
        "--chart-2": "#f97316",
        "--chart-3": "#ea580c",
        "--chart-4": "#dc2626",
        "--chart-5": "#b91c1c",
        "--sidebar": "#0c0a09",
        "--sidebar-foreground": "#fafaf9",
        "--sidebar-primary": "#f97316",
        "--sidebar-primary-foreground": "#ffffff",
        "--sidebar-accent": "#9a3412",
        "--sidebar-accent-foreground": "#fafaf9",
        "--sidebar-border": "#292524",
        "--sidebar-ring": "#f97316",
      },
    },
  },
];

export function applyTheme(themeName: string) {
  const theme = themes.find(t => t.name === themeName);
  if (!theme) return;

  const root = document.documentElement;
  const isDark = document.documentElement.classList.contains('dark');
  const colors = isDark ? theme.colors.dark : theme.colors.light;

  Object.entries(colors).forEach(([property, value]) => {
    root.style.setProperty(property, value);
  });
}

export function getCurrentTheme(): string {
  return localStorage.getItem('theme') || 'default';
}

export function setCurrentTheme(themeName: string) {
  localStorage.setItem('theme', themeName);
  applyTheme(themeName);
} 